package cn.edu.nwafu.core.controller.api;


import cn.edu.nwafu.common.exception.Assert;
import cn.edu.nwafu.common.exception.BusinessException;
import cn.edu.nwafu.common.result.R;
import cn.edu.nwafu.common.result.ResponseEnum;
import cn.edu.nwafu.core.pojo.entity.UserInfo;
import cn.edu.nwafu.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhanghao
 * @since 2021-10-30
 */
@RestController
@Slf4j
@RequestMapping("/api/core/userInfo")
@Api(tags = "用户管理")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("用户注册")
    @PostMapping("/register/{code}")
    public R register(
            @ApiParam("用户注册信息")
            @RequestBody(required = true) UserInfo userInfo,
            @ApiParam("用户验证码")
            @PathVariable String code
    ) {

        String idCard = userInfo.getIdCard();
        String mobile = userInfo.getMobile();
        String mail = userInfo.getMail();

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", idCard);
        int count = userInfoService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ResponseEnum.IDCARD_EXIST_ERROR);
        }

        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        count = userInfoService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ResponseEnum.MOBILE_EXIST_ERROR);
        }

        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mail", mail);
        count = userInfoService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ResponseEnum.MAIL_EXIST_ERROR);
        }

        String codeGen = (String) redisTemplate.opsForValue().get("airline:registration:code:" + mail);
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);

        boolean save = userInfoService.save(userInfo);
        if (save) {
            return R.ok().message("用户注册成功");
        } else {
            throw new BusinessException(ResponseEnum.REGISTER_ERROR);
        }
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(
            @ApiParam("用户登录信息")
            @RequestBody UserInfo userInfo
    ) {

        String mobile = userInfo.getMobile();
        String password = userInfo.getPassword();

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile).eq("password", password);

        UserInfo user = userInfoService.getOne(queryWrapper);
        if (user == null) {
            return R.error().message("用户名或密码输入错误");
        }

        return R.ok().data("user", user);
    }

}

